package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.OrdersDao;
import com.itheima.domain.store.Orders;
import com.itheima.domain.store.Product;
import com.itheima.domain.system.Role;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.OrdersService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {


    @Override
    public void save(Orders orders) {

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            OrdersDao ordersDao = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            //3.调用Dao层操作

            if (ids != null) {
                for (String id : ids) {
                    ordersDao.delete(id);
                    ordersDao.deletePo(id);
                    //4.提交事务
                    TransactionUtil.commit(sqlSession);
                }
            }


        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    public void update(Orders orders) {

    }

    @Override
    public Orders findById(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            OrdersDao ordersDao = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            Orders orders = ordersDao.findById(id);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM--dd");
            Product product = orders.getProduct();
            Date departureTime = product.getDepartureTime();
            Date orderTime = orders.getOrderTime();
            String dateStr = sdf.format(departureTime);
            String orderTimeStr = sdf.format(orderTime);
            product.setDepartureTimeStr(dateStr);
            orders.setProduct(product);
            orders.setOrderTimeStr(orderTimeStr);
            /*Date orderTime = orders.getOrderTime();
            orders.setOrderTimeStr(datas.format(orderTime));
            Product product = orders.getProduct();
            Date departureTime = product.getDepartureTime();
            product.setDepartureTimeStr(datas.format(departureTime));*/
            return orders;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<Orders> findAll() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            OrdersDao ordersDao = MapperFactory.getMapper(sqlSession, OrdersDao.class);

            return ordersDao.findAll();

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            OrdersDao ordersDao = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            PageHelper.startPage(page, size);
            List<Orders> list = ordersDao.findAll();
            for (Orders orders : list) {
                Date orderTime = orders.getOrderTime();
                SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String format = sp.format(orderTime);
                orders.setOrderTimeStr(format);
            }
            PageInfo pageInfo = new PageInfo(list);
            return pageInfo;

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void deletePo(String[] ids) {
        SqlSession sqlSession = null;
        try {
            //1.获取SqlSession
            sqlSession = MapperFactory.getSqlSession();
            //2.获取Dao
            OrdersDao ordersDao = MapperFactory.getMapper(sqlSession, OrdersDao.class);
            //3.调用Dao层操作

            if (ids != null) {
                for (String id : ids) {
                    System.out.println(id);
                    ordersDao.deletePo(id);
                    //4.提交事务
                    TransactionUtil.commit(sqlSession);
                }
            }


        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
